package cn.com.surker.material.controller;

import cn.com.surker.common.AbstractController;
import cn.com.surker.dto.main.MaterialWeightQueryDTO;
import cn.com.surker.material.entity.MainMaterialWeight;
import cn.com.surker.material.service.impl.MaterialWeightServiceImpl;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.main.MainMaterialWeightVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author lucky
 * @create 2024年10月10 9:07
 * @desc 原材过磅表
 */
@RestController
@Controller("material-weight-erp")
@RequestMapping("/material-weight")
public class MaterialWeightController extends AbstractController {

    @Resource
    private MaterialWeightServiceImpl materialWeightService;

    /**
     * 分页查询过磅数据
     * @param searchVal 检索条件
     * @param orgId 公司ID
     * @param supplierId 供应商ID
     * @param materialId 材料ID
     * @param weightType 过磅类型 1原材2辅材3商砼退料4砼过磅5登记皮重6原材出售过磅0其它过磅
     * @param isPrint 是否打印
     * @param isDisplay 是否显示
     * @param isBill 是否结算
     * @param startTime
     * @param endTime
     * @param limit
     * @param searchType 0 已完成过磅， 1 未完成过磅
     * @param page
     * @param limit
     * @param curr
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResultDto page(String searchVal, Long orgId, Long supplierId, Long materialId, Integer weightType,
                              Integer isPrint, Integer isDisplay, Integer isBill, String startTime, String endTime,
                              Long storageId, Integer status, Integer searchType, Integer carType, Integer isCheck,
                              Integer page, Integer limit, Integer curr){
        try {
            /**分页对象**/
            if (!StringUtils.isNull(curr))//layui不能用page传页码数则修改默认页码参数传入
                page = curr;
            else {
                if (StringUtils.isNull(page))
                    page = 1;
            }
            if (StringUtils.isNull(limit))
                limit = 15;
            SysUser user = getUser();
            if (user == null || user.getStatus() == 1)
                return RestResultDto.fail("登录信息失效,请重新登录");
            if (StringUtils.isNull(searchType))
                searchType = 1;
            if (StringUtils.isNull(isDisplay))
                isDisplay = 1;

            MainMaterialWeightVO materialWeightVO = new MainMaterialWeightVO();
            materialWeightVO.setSearchVal(searchVal);
            materialWeightVO.setOrgId(orgId);
            materialWeightVO.setSupplierId(supplierId);
            materialWeightVO.setMaterialId(materialId);
            materialWeightVO.setWeightType(weightType);
            materialWeightVO.setIsPrint(isPrint);
            materialWeightVO.setIsDisplay(isDisplay);
            materialWeightVO.setIsBill(isBill);
            materialWeightVO.setStartTime(startTime);
            materialWeightVO.setEndTime(endTime);
            materialWeightVO.setStorageId(storageId);
            materialWeightVO.setStatus(status);
            materialWeightVO.setSearchType(searchType);
            materialWeightVO.setCarType(carType);
            materialWeightVO.setIsCheck(isCheck);
            IPage iPage = materialWeightService.findPage(materialWeightVO, 1,(page - 1) * limit, limit);
            return RestResultDto.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,请稍后再试");
        }
    }

    /**
     * 新增过磅数据
     * @param materialWeight
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto save(@RequestBody MainMaterialWeight materialWeight){
        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");
            if (materialWeight == null)
                return RestResultDto.fail("参数错误");
            materialWeight.setCreator(sysUser.getName());
            materialWeight.setUpdator(sysUser.getName());

            return materialWeightService.save(materialWeight);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }

    /**
     * 修改过磅单数据
     * @param materialWeight
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RestResultDto update(@RequestBody MainMaterialWeight materialWeight){
        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");
            if (materialWeight == null)
                return RestResultDto.fail("参数错误");
            materialWeight.setUpdator(sysUser.getName());

            return materialWeightService.update(materialWeight);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }

    /**
     * 修改过磅单状态
     * @param id
     * @param status
     * @param remark
     * @return
     */
    @RequestMapping(value = "/update-status", method = RequestMethod.POST)
    public RestResultDto updateStatus(Long id, Integer status, String remark){
        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");

            return materialWeightService.updateStatus(id, status, remark, sysUser.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    /**
     * 设置仓位信息
     * @param id
     * @param storageId
     * @param remark
     * @return
     */
	@RequestMapping(value = "/set-storage/{id}", method = RequestMethod.PUT)
    public RestResultDto setStorage(@PathVariable Long id, Long storageId, String remark){
        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");

            return materialWeightService.setStorage(id, storageId, remark, sysUser.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }

    /**
     * 设置扣重
     * @param id
     * @param hiddenWeight
     * @param brightWeight
     * @param remark
     * @return
     */
    @RequestMapping(value = "/set-weight/{id}", method = RequestMethod.PUT)
    public RestResultDto setWeight(@PathVariable Long id, Double hiddenWeight, Double brightWeight, String remark){
        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");

            return materialWeightService.setWeight(id, hiddenWeight, brightWeight, remark, sysUser.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }

    /**
     * 审核过磅信息
     * @param ids
     * @param remark
     * @param type
     * @return
     */
    @RequestMapping(value = "/check-weight", method = RequestMethod.PUT)
    public RestResultDto checkWeight(Long  orgId,String ids, String remark,Integer type){
        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");

            return materialWeightService.checkWeight(orgId, ids, sysUser.getName(), remark, type);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }


    /**
     * 汽车过磅记录 weight_type   3商砼退料 4砼过磅 5登记皮重
     * @param dto
     * @return
     */
    @PostMapping(value = "/carpage")
    @ApiOperation(value = "分页方法", tags = "汽车过磅记录")
    public RestResultDto pageData(@RequestBody MaterialWeightQueryDTO dto){
        return RestResultDto.success(materialWeightService.pageData(dto));
    }


}
